#! /bin/make -f
# @file
# @brief Makefile规则 (eppc-e200zx-codewarrior)
# @author
# + 隐星魂 (Roy.Sun) <www.starsoul.tech>
# @copyright
# + (c) 2015 隐星魂 (Roy.Sun) <www.starsoul.tech>
# > Licensed under the Apache License, Version 2.0 (the "License");
# > you may not use this file except in compliance with the License.
# > You may obtain a copy of the License at
# >
# >         http://www.apache.org/licenses/LICENSE-2.0
# >
# > Unless required by applicable law or agreed to in writing, software
# > distributed under the License is distributed on an "AS IS" BASIS,
# > WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# > See the License for the specific language governing permissions and
# > limitations under the License.
#

include $(XWOS_BRD_DIR)/cfg/ld.cfg

CWMCUEPPC_PATH ?= c:/app/cwmcu-eppc/
CLTDIR := $(CWMCUEPPC_PATH)/PA_Tools/Command_Line_Tools
WINEPREFIX ?= $(shell pwd)/../tool/cwmcu-eppc-wine
WINE ?= wine
CWMCUEPPC_LICENSE ?=
ifeq ($(WINEPREFIX),)
    CC := $(WINE) $(CLTDIR)/mwcceppc.exe
    AS := $(WINE) $(CLTDIR)/mwasmeppc.exe
    OD := $(WINE) $(CLTDIR)/mwcceppc.exe -disassemble
    LD := $(WINE) $(CLTDIR)/mwldeppc.exe -application
    AR := LM_LICENSE_FILE=$(CWMCUEPPC_LICENSE) $(WINE) $(CLTDIR)/mwldeppc.exe -library
else
    CC := WINEPREFIX=$(WINEPREFIX) $(WINE) $(CLTDIR)/mwcceppc.exe
    AS := WINEPREFIX=$(WINEPREFIX) $(WINE) $(CLTDIR)/mwasmeppc.exe
    OD := WINEPREFIX=$(WINEPREFIX) $(WINE) $(CLTDIR)/mwcceppc.exe -disassemble
    LD := WINEPREFIX=$(WINEPREFIX) $(WINE) $(CLTDIR)/mwldeppc.exe -application
    AR := WINEPREFIX=$(WINEPREFIX) LM_LICENSE_FILE=$(CWMCUEPPC_LICENSE) $(WINE) $(CLTDIR)/mwldeppc.exe -library
endif
LUA2HEX := python3 xwbs/util/py/lua2hex.py
RM := -rm

# build flags
ifeq ($(ARCHCFG_BIG_ENDIAN),y)
    ENDIAN := big
else
    ENDIAN := little
endif
~D ?= 1
WARN := -warn illpragmas -warn unwanted -warn hidevirtual -warn implicitconv \
        -warn impl_int2float -warn impl_float2int -warn impl_signedunsigned \
        -warn ptrintconv -warn unusedarg -warn unusedvar -warn missingreturn \
        -warn unusedexpr -warn extracomma -warn structclass

INCDIRS := -I. \
           -I$(XWOS_ARCH_DIR) \
           -I$(XWOS_CPU_DIR) \
           -I$(XWOS_SOC_DIR) \
           -I$(XWOS_BRD_DIR) \
           -i $(CWMCUEPPC_PATH)/PA_Support/ewl/EWL_C/include

PREFIX = $(XWOS_BRD_DIR)/cfg/board.prefix

CFLAGS = -sym dwarf-2 -sym full -msgstyle gcc -proc zen -$(ENDIAN) -fp soft \
         -model absolute -abi eabi -sdata 8 -sdata2 8 -vle -enc ascii -c \
         -stdinc -nosyspath -inline on -inline nobottomup \
         -align powerpc -func_align 4 -pooldata off -common off -use_lmw_stmw on \
         -novolatileasm -schedule off -opt nopeephole -use_isel on -ppc_asm_to_vle \
         -Cpp_Exceptions off -bool on -requireprotos -lang c99 -gccext on \
         -char unsigned -str nopool -str reuse -ipa off -prefix $(PREFIX) $(WARN) \
         -MD -gccdep
ifeq ($(~D),0)
    CFLAGS += -opt level=2 -opt speed
else
    CFLAGS += -opt level=0 -opt speed
endif

AFLAGS = -sym dwarf-2 -sym full -msgstyle gcc -proc zen -$(ENDIAN) -vle -nosyspath \
         -D__ASM__=1

LDFLAGS = -sym dwarf-2 -sym full -msgstyle gcc -proc zen -$(ENDIAN) -fp soft \
          -model absolute -abi eabi -sdata 8 -sdata2 8 \
          -lcf "$(XWOS_BRD_DIR)/$(XuanWuOS_CFG_LDSCRIPT)" -m arch_isr_reset \
          -L"$(CWMCUEPPC_PATH)/PA_Support/ewl/lib" -char unsigned \
          -heapsize 32 -stacksize 32 -map -listclosure -mapunused \
          -genbinary none -srec -sreclength 26 -sreceol dos \
          -romaddr $(ROMADDR) -rambuffer $(RAMBUFFER)

OBJ_DIR := $(XWOS_WKSPC_DIR)/obj/

include $(XWOS_ARCH_DIR)/arch.mk
include $(XWOS_CPU_DIR)/cpu.mk
include $(XWOS_SOC_DIR)/soc.mk
include $(XWOS_BDL_DIR)/bdl.mk
include $(XWOS_KN_DIR)/xwos.mk
include $(XWBS_UTIL_MK_XWMO)

# colorfully build
ifneq ($(~V),1)
    SHOW_CPP = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[cc -E] "$<" -> "$@" \e[0m";
    SHOW_CC = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[cc] "$<" -> "$@" \e[0m";
    SHOW_APP = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[as -E] "$<" -> "$@" \e[0m";
    SHOW_AS = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[as] "$<" -> "$@" \e[0m";
    SHOW_OD = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[od] "$<" -> "$@" \e[0m";
    SHOW_CP = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[cp] "$<" -> "$@" \e[0m";
    SHOW_MM = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[mm] "$<" -> "$@" \e[0m";
    SHOW_AR = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[ar] "$@" \e[0m";
    SHOW_LD = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[ld] "$@" \e[0m";
    SHOW_MOT2BIN = echo -e "\e[1m\e[4$$[RANDOM & 7];36m[mot2bin] "$@" \e[0m";
    SHOW_LUA2HEX = @echo -e "\e[1m\e[4$$[RANDOM & 7];36m[lua2hex] "$@" \e[0m";
else
    SHOW_CPP =
    SHOW_CC =
    SHOW_APP =
    SHOW_AS =
    SHOW_OD =
    SHOW_CP =
    SHOW_MM =
    SHOW_AR =
    SHOW_LD =
    SHOW_MOT2BIN =
    SHOW_LUA2HEX =
endif
